package com.huiztech.kaoqinji;

import java.util.Date;

import com.huiztech.kaoqinji.model.Remark;
import com.huiztech.kaoqinji.mvc.KaoqinBaseActivity;
import com.huiztech.kaoqinji.mvc.KaoqinController;
import com.huiztech.util.FormatTime;
import com.lidroid.xutils.util.LogUtils;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddRemarkActivity extends KaoqinBaseActivity {
    TextView sendBtn;
    EditText contentEdit;
    String content="";
    int id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_remark);
        sendBtn = (TextView) findViewById(R.id.add_remark_ok_text);
        contentEdit = (EditText) findViewById(R.id.add_remark_content_edit);
        id=getIntent().getIntExtra("id",0);
        if(id!=0){
            content=getIntent().getStringExtra("content");
            contentEdit.setText(content);
        }
        sendBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(content.equals("")){
                    Remark remark = new Remark();
                    long data = FormatTime.getMillis(new Date());
                    LogUtils.i(data+"+");
                    String contentString = contentEdit.getText().toString();
                    if(contentString.equals("")){
                        Toast.makeText(AddRemarkActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                    }else{
                        remark.setStoreTime(data);
                        remark.setContent(contentString);
                        controller.storeRemarkToDB(remark);
                        Toast.makeText(AddRemarkActivity.this, "插入成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                }else {
                    Remark remark = new Remark();
                    long data = FormatTime.getMillis(new Date());
                    String contentString = contentEdit.getText().toString();
                    if(contentString.equals("")){
                        Toast.makeText(AddRemarkActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                    }else{
                        remark.setId(id);
                        remark.setStoreTime(data);
                        remark.setContent(contentString);
                        controller.storeRemarkToDB(remark);
                        contentEdit.setSelection(contentEdit.length());//
                        Toast.makeText(AddRemarkActivity.this, "编辑成功", Toast.LENGTH_SHORT).show();
                        finish();

                    }
                                    }

            }
        });
    }
}
